using System;
using System.Globalization;
using ns20;

namespace Newtonsoft.Json.Converters
{
	public class JavaScriptDateTimeConverter : DateTimeConverterBase
	{
		public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
		{
			long value2;
			if (value is DateTime)
			{
				DateTime dateTime_ = ((DateTime)value).ToUniversalTime();
				value2 = Class185.smethod_8(dateTime_);
			}
			else
			{
				if (!(value is DateTimeOffset))
				{
					throw new JsonSerializationException("Expected date object value.");
				}
				value2 = Class185.smethod_8(((DateTimeOffset)value).ToUniversalTime().UtcDateTime);
			}
			writer.WriteStartConstructor("Date");
			writer.WriteValue(value2);
			writer.WriteEndConstructor();
		}

		public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
		{
			Type type = (Class195.smethod_10(objectType) ? Nullable.GetUnderlyingType(objectType) : objectType);
			if (reader.JsonToken_0 == JsonToken.Null)
			{
				if (!Class195.smethod_9(objectType))
				{
					throw JsonSerializationException.smethod_1(reader, "Cannot convert null value to {0}.".smethod_0(CultureInfo.InvariantCulture, objectType));
				}
				return null;
			}
			if (reader.JsonToken_0 == JsonToken.StartConstructor && string.Equals(reader.Object_0.ToString(), "Date", StringComparison.Ordinal))
			{
				reader.Read();
				if (reader.JsonToken_0 != JsonToken.Integer)
				{
					throw JsonSerializationException.smethod_1(reader, "Unexpected token parsing date. Expected Integer, got {0}.".smethod_0(CultureInfo.InvariantCulture, reader.JsonToken_0));
				}
				long long_ = (long)reader.Object_0;
				DateTime dateTime = Class185.smethod_11(long_);
				reader.Read();
				if (reader.JsonToken_0 != JsonToken.EndConstructor)
				{
					throw JsonSerializationException.smethod_1(reader, "Unexpected token parsing date. Expected EndConstructor, got {0}.".smethod_0(CultureInfo.InvariantCulture, reader.JsonToken_0));
				}
				if (type == typeof(DateTimeOffset))
				{
					return new DateTimeOffset(dateTime);
				}
				return dateTime;
			}
			throw JsonSerializationException.smethod_1(reader, "Unexpected token or value when parsing date. Token: {0}, Value: {1}".smethod_1(CultureInfo.InvariantCulture, reader.JsonToken_0, reader.Object_0));
		}
	}
}
